//
//  ActivityDetailViewController.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/26.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "ActivityDetailViewController.h"

#define CAT_BUTTON_WIDTH MAIN_SCREEN_WIDTH / 5

@interface ActivityDetailViewController () {
    
    NSMutableArray *catButtonArray;

    UIView *indicatorView;
    
    UIScrollView *scrollView;
    
    ActivityDetailTableView *activityDetailTableView;
    ActivityDetailWebView *activityDetailWebView;
    ActivityDetailCommentView *activityDetailCommentView;
    
    ActivityModel *activity;
    
     int height ;
}

@end

@implementation ActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self->catButtonArray = NSMutableArray.new;
    
    [self drawView];
    [self getActivityDetail];

}

- (void) drawView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *navigationBar = UIView.new;
    [self.view addSubview:navigationBar];
    
    UIView *rootView = self.view;

    
    
     height = NAVIGATION_BAR_HEIGHT;
    int titleLabelOffset = 20;
    if (DEVICE_IS_IPHONEX) {
        height = NAVIGATION_BAR_X_HEIGHT;
        titleLabelOffset = 40;
    }
    
    [navigationBar makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rootView.top).offset(0);
        make.left.equalTo(rootView.left).offset(0);
        make.right.equalTo(rootView.right).offset(0);
        make.height.equalTo(self->height);
    }];
    
    ///返回
    UIImageView *backImageview = UIImageView.new;
    backImageview.image = [UIImage imageNamed:@"back"];
    [navigationBar addSubview:backImageview];
    
    
    [backImageview makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.top).offset(self->height-30);
        make.left.equalTo(navigationBar.left).offset(15);
        make.width.equalTo(11.5);
        make.height.equalTo(21);
    }];
    
    // 返回button
    UIButton *backButton = UIButton.new;
    [navigationBar addSubview:backButton];
    
    [backButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.top).offset(20);
        make.left.equalTo(navigationBar.left).offset(10);
        make.width.equalTo(44);
        make.height.equalTo(44);
    }];
    
    [backButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSArray *catArray =  [NSArray arrayWithObjects: @"商品", @"详情", @"评论", nil] ;
    
    for(int i=0; i<catArray.count; i++) {
        UIButton *tempButton = UIButton.new;
        tempButton.tag = i;
        [tempButton setTitle:[catArray objectAtIndex:i] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize: 18];
        [tempButton setTitleColor:MAIN_FONT_COLOR forState:UIControlStateNormal];
        [tempButton setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
        [tempButton addTarget:self action:@selector(clickCatButton:) forControlEvents:UIControlEventTouchUpInside];
        [navigationBar addSubview:tempButton];
        
        [tempButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(navigationBar.bottom).offset(0);
            make.left.equalTo(navigationBar.left).offset(CAT_BUTTON_WIDTH*(i+1));
            make.size.equalTo(CGSizeMake(CAT_BUTTON_WIDTH, 40));
        }];
        
        if (i == 0) {
            tempButton.selected = YES;
        }
        
        [self->catButtonArray addObject:tempButton];
    }
    
   // NSLog(@"scrollViewPage=%lu", (unsigned long)self->catButtonArray.count);
    
    
    self->indicatorView = [[UIView alloc] initWithFrame:CGRectMake(CAT_BUTTON_WIDTH+CAT_BUTTON_WIDTH/2-40/2, height-2, 40, 2)];
    self->indicatorView.backgroundColor = MAIN_COLOR;
    [self.view addSubview:self->indicatorView];
    
    //分割线
    UIView *divideLine = UIView.new;
    divideLine.backgroundColor = UIColorFromRGB(0xB2B2B2) ;
    [navigationBar addSubview:divideLine];
    
    [divideLine makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(navigationBar.left).offset(0);
        make.right.equalTo(navigationBar.right).offset(0);
        make.bottom.equalTo(navigationBar.bottom);
        make.height.equalTo(1/[[UIScreen mainScreen] scale]);  // 一个像素在所有的屏幕上
    }];
    
    
    self->scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, height, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - height)];
    
    
    self->scrollView.pagingEnabled = YES;  // 控制控件是否整页翻动
    self->scrollView.showsHorizontalScrollIndicator = NO;
    self->scrollView.showsVerticalScrollIndicator = NO;
    self->scrollView.bounces = NO;
    self->scrollView.delegate = self;
    //self->scrollView.hidden = YES;
    [self.view addSubview:self->scrollView];
    
    for (int i=0; i<3; i++) {
        
        if (i == 0) {
            self->activityDetailTableView = [[ActivityDetailTableView alloc] initWithFrame:CGRectMake(MAIN_SCREEN_WIDTH * i, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 64)];
            [self->scrollView addSubview:self->activityDetailTableView];
            
        } else if (i == 1) {
            self->activityDetailWebView = [[ActivityDetailWebView alloc] initWithFrame:CGRectMake(MAIN_SCREEN_WIDTH * i, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 64)];
            [self->scrollView addSubview:self->activityDetailWebView];
            
        } else {
            
            self->activityDetailCommentView = [[ActivityDetailCommentView alloc] initWithFrame:CGRectMake(MAIN_SCREEN_WIDTH * i, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 64) :self.view];
            [self->scrollView addSubview:self->activityDetailCommentView];
            
        }
    }
    
    self->scrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH * 3, MAIN_SCREEN_HEIGHT - 64);
    
    self->scrollView.contentOffset = CGPointZero;
    
}

- (void) clickCatButton:(UIButton *) button {
    
    if (button.selected == YES) {
        return;
    }
    
    for (int i=0; i<self->catButtonArray.count; i++) {
        UIButton *tempButton = [self->catButtonArray objectAtIndex:i];
        if (tempButton.tag == button.tag) {
            tempButton.selected = YES;
        } else {
            tempButton.selected = NO;
        }
    }
    
    [self setScrollViewSetContentOffset:(int)button.tag];
    
}

// 滚动就触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
   
    [self->indicatorView setFrame:CGRectMake((scrollView.contentOffset.x / MAIN_SCREEN_WIDTH) * CAT_BUTTON_WIDTH + CAT_BUTTON_WIDTH+CAT_BUTTON_WIDTH/2-20, height-2, self->indicatorView.frame.size.width, self->indicatorView.frame.size.height)];
}

// 减速停止了时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    int scrollViewPage = (int) (scrollView.contentOffset.x/MAIN_SCREEN_WIDTH);
    
    for (int i=0; i<self->catButtonArray.count; i++) {
        UIButton *tempButton = [self->catButtonArray objectAtIndex:i];
        if (tempButton.tag == scrollViewPage) {
            tempButton.selected = YES;
        } else {
            tempButton.selected = NO;
        }
    }
}

- (void) setScrollViewSetContentOffset:(int)currentPage
{
    [self->scrollView setContentOffset:CGPointMake(MAIN_SCREEN_WIDTH * currentPage, 0) animated:NO];
}

- (void)getActivityDetail {
    
    NSMutableDictionary * params = NSMutableDictionary.new;
    [params setValue:[NSString stringWithFormat:@"%d",self.id] forKey:@"id"];
    
    [HttpHelper request:@"activity/detail" :@"GET" :params successBlock:^(NSDictionary *responseDic) {
        
        //这里解析数据要将解析的格式写在前面，否则会导致数据仍然解析为空
        [GoodsModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"images" : @"ImageModel"};
        }];
        
        [CommentModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"images" : @"ImageModel"};
        }];
        
        [ActivityModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"default_comments" : @"CommentModel", @"comments" : @"CommentModel"};
        }];
        
       self->activity = [ActivityModel mj_objectWithKeyValues:responseDic[@"data"]];
    
        //NSLog(@"%@",self->activity.goods.images);
        
        [self->activityDetailTableView initView:self->activity];
        [self->activityDetailWebView initView:self->activity];
        [self->activityDetailCommentView initView:self->activity];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"请求出错");
    
    }];
}


-(void) clickBack {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
